package cn.tedu.csmall.passport.controller;

import cn.tedu.csmall.commons.pojo.vo.PageData;
import cn.tedu.csmall.passport.pojo.param.AdminAddNewParam;
import cn.tedu.csmall.passport.pojo.param.AdminLoginInfoParam;
import cn.tedu.csmall.passport.pojo.param.AdminUpdateInfoParam;
import cn.tedu.csmall.passport.pojo.vo.AdminListItemVO;
import cn.tedu.csmall.passport.pojo.vo.AdminStandardVO;
import cn.tedu.csmall.commons.security.LoginPrincipal;
import cn.tedu.csmall.passport.service.AdminService;
import cn.tedu.csmall.commons.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;

@RestController
@RequestMapping("/admin")
@Slf4j
@Api(tags = "管理员模块")
@Validated
public class AdminController {

    @Autowired
    AdminService adminService;

    @PostMapping("/login")
    @ApiOperation("管理员登陆")
    @ApiOperationSupport(order = 1)
    public JsonResult login(AdminLoginInfoParam adminLoginInfoParam) {
        log.debug("开始处理【管理员登陆】请求，请求参数：{}", adminLoginInfoParam);
        String jwt = adminService.login(adminLoginInfoParam);
        log.debug("处理【管理员登陆】请求完毕");
        return JsonResult.ok(jwt);
    }

    @PostMapping("/add-new")
    @ApiOperation("添加管理员")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(AdminAddNewParam adminAddNewParam) {
        log.debug("controller收到请求，开始转交给service");
        adminService.addNew(adminAddNewParam);
        log.debug("开始响应数据！");
        return JsonResult.ok();
    }

    @PostMapping("/delete")
    @ApiOperation("根据id删除数据")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "管理员ID",required = true,dataType = "long"),

    })
    public JsonResult delete(@Range(min = 1,message = "请提交合法的ID值！")
                             @RequestParam Long id) {
        log.debug("开始处理【根据id删除管理员】的请求，参数：{}", id);
        adminService.delete(id);
        log.debug("处理【根据id删除管理员】的请求，完成！");
        return JsonResult.ok();
    }

    @PostMapping("/update")
    @ApiOperation("修改管理员")
    @ApiOperationSupport(order = 300)
    public JsonResult updateInfoById(@Range(min = 1,message = "请提交合法的ID值！")
                                     @RequestParam Long id,
                                     @Valid AdminUpdateInfoParam adminUpdateInfoParam) {
        log.debug("开始处理【修改管理员】的请求，参数：{}", id, adminUpdateInfoParam);
        adminService.updateInfoById(id, adminUpdateInfoParam);
        log.debug("处理【修改管理员】的请求，完成！");
        return JsonResult.ok();
    }

    @GetMapping("/list")
    @ApiOperation("查询管理员列表")
    @ApiOperationSupport(order = 400)
    public JsonResult list(Integer pageNum,@ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal) {
        log.debug("controller收到请求，开始转交给service");
        String s = loginPrincipal.getUsername();
        Long i = loginPrincipal.getId();
        log.debug(s);
        log.debug("id为{}", i);
        PageData<AdminListItemVO> list = adminService.list(pageNum);
        return JsonResult.ok(list);
//        PageData<AdminListItemVO> pageData = adminService.list(pageNum);
//        log.debug("开始响应数据！");
//        return JsonResult.ok(pageData);
    }

    @GetMapping("/standard")
    @ApiOperation("根据id查询管理员")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "管理员id",required = true,dataType = "Long",paramType = "query")
    })
    public JsonResult getStandard(Long id) {
        log.debug("开始处理【根据id查询管理员】的请求，id：{}",id);
        AdminStandardVO standardById = adminService.getStandardById(id);
        return JsonResult.ok(standardById);
    }
}
